package com.david.service;

import com.david.model.Person;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.concurrent.ConcurrentHashMap;

@Service
public class PersonService {

    static ConcurrentHashMap<Integer,Person> map = new ConcurrentHashMap<>();

    static {
        for (int i = 0; i < 100; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName("david"+i);
            map.put(i,person);
        }
    }

    public Person getPerson(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("getPerson"+Thread.currentThread().getName());
        return map.get(1);
    }

    public Person getPersonMax(){
        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map.get(1);
    }

    public Flux<Person> getPersons(){
        //响应式数据源
        //观察者是Flux，被观察者是map中的数据
        Flux<Person> result = Flux.fromIterable(map.values());
        return result;
    }
}
